class MigrateDocuments < ActiveRecord::Migration

  class << self
    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    # Database create/drop methods
    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   
    def up
      create_document_categories_table
      create_documents_table
      create_document_versions_table
    end

    def down
      drop_document_versions_table
      drop_documents_table
      drop_document_categories_table
    end
    
    private
      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      # Table create/drop methods
      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      
      def create_document_categories_table
        create_table_with_history(:document_categories, :force => true) do |t|
          t.column("name", :text, :null => false)
          t.column("title", :text, :null => false)
          t.column("description", :text)
          t.column("updated_by", :integer)
          t.column("updated_at", :datetime)
        end

        create_lookup_table(:document_categories,:pages)
      end

      def drop_document_categories_table
        drop_lookup_table(:document_categories,:pages)
        drop_table_with_history(:document_categories)
      end

      def create_documents_table
        create_table_with_privies(:documents, :force => true) do |t|
          t.column("title", :text, :null => false)
          t.column("document_category_id", :integer, :null => false)
          t.column("updated_by", :integer, :null => false)
          t.column("updated_at", :datetime)
        end

        foreign_key(:documents, :document_category_id, :document_categories)
        foreign_key(:documents, :updated_by, :updaters)
      end

      def drop_documents_table
        drop_table(:documents)
      end

      def create_document_versions_table
        create_table_with_privies(:document_versions, :force => true) do |t|
          t.column("document_id", :integer)
          t.column("is_current", :boolean, :default => false)
          t.column("name", :text, :null => false)
          t.column("location", :text, :null => false)
          t.column("kilobytes", :float, :null => false)
          t.column("version", :integer, :default => 1)
          t.column("updated_by", :integer, :null => false)
          t.column("updated_at", :datetime)
        end

        foreign_key(:document_versions, :document_id, :documents)
        foreign_key(:document_versions, :updated_by, :updaters)
      end
      
      def drop_document_versions_table
        drop_table(:document_versions)
      end
  end #end class block
end
